home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / VOL_400 / 447_01 / FORMEDIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-22  |  5.3 KB  |  163 lines

  1. #include "relay.h"
  2.  
  3. int formedit( struct datatable t[], struct window win )
  4.  
  5. /* This function controls cursor movement within a form being edited.      */
  6. /* Parameters: t[] an array of datatable structures containing the         */
  7. /*             information required to build the form on screen.           */
  8. /*             win a window structure containing data about the window for */
  9. /*             the form in t[].                                            */
  10. /* Return value: key an int corresponding to a key that terminated editing */
  11. /*               of the form.                                              */
  12.  
  13. {
  14.  
  15. int i = 0, j = 0, kontinue, maxx = 0 , formlength, key = 0;
  16. char c, buff[10];
  17. struct datatable form[40];
  18.  
  19.     while(  (t[i].descrip[0] != 'Q') &&
  20.             (t[i].descrip[1] != 'Q') &&
  21.             (t[i].descrip[2] != 'Q')){
  22.         while (t[i].type == 0)     /* Copy t[] into form[] without titles. */
  23.             i++;
  24.         form[j++] = t[i++];
  25.         maxx++;
  26.     }
  27.     formlength = i;
  28.  
  29.     _settextwindow( win.upl.x, win.upl.y, win.lowr.x, win.lowr.y);
  30.     _setbkcolor(text.background);
  31.     _clearscreen( _GWINDOW );
  32.     _settextcolor(text.input);
  33.  
  34. /***************************** Print form. *********************************/
  35.  
  36.     for (i = 0; i < formlength; i++){
  37.         _settextposition( t[i].xtext, t[i].ytext );
  38.         _outtext( t[i].descrip );
  39.  
  40.         switch(t[i].type){
  41.             case 0:                                      /* Title.         */
  42.                 _settextposition(t[i].xtext, t[i].yinput + t[i].ytext );
  43.                 _outtext( t[i].indata );
  44.                 _settextposition( 1, 1 );
  45.                 break;
  46.             case 1:                                      /* Text field.    */
  47.                 _settextposition(t[i].xtext, t[i].yinput + t[i].ytext );
  48.                 _outtext( t[i].indata );
  49.                 _settextposition( 1, 1 );
  50.                 break;
  51.             case 2:                                      /* Integer field. */
  52.                 sprintf( buff, t[i].format, atol(t[i].indata));
  53.                 _settextposition(t[i].xtext, t[i].yinput + t[i].ytext );
  54.                 _outtext( buff );
  55.                 break;
  56.  
  57.             case 3:                                      /* Float field.   */
  58.                 sprintf( buff, t[i].format, atof(t[i].indata));
  59.                 _settextposition(t[i].xtext, t[i].yinput + t[i].ytext );
  60.                 _outtext( buff );
  61.                 break;
  62.         }
  63.     }
  64.  
  65.     _settextcolor( text.helpc );              /* Print help line for form. */
  66.     _settextposition( text.helpp, 2 );
  67.     _outtext( win.helpbuf );
  68.     _settextcolor( text.input );
  69.  
  70. /******************************* Edit form. ********************************/
  71.  
  72.     i = 0;
  73.     cursor(form[i], ON, win);/* Initialize cursor position to first field. */
  74.     kontinue = TRUE;
  75.     while(kontinue){                                           /* Get key. */
  76.         c = getch();
  77.         if ( (c >= 'a' && c <= 'z') ||     /* Test c to determine if it is */
  78.              (c >= 'A' && c <= 'Z') ||     /* a letter, number, or '.'.    */
  79.              (c >= '0' && c <= '9') ||
  80.              (c == '.') ){
  81.             ungetch(c);
  82.             strcpy( form[i].indata, intext( form[i], win ) );
  83.             if (i != (maxx-1)){
  84.                 i++;
  85.                 cursor( form[i-1], OFF, win );
  86.             }
  87.             cursor( form[i], ON, win );
  88.             _settextwindow( win.upl.x, win.upl.y,     /* Reprint help line */
  89.                             win.lowr.x, win.lowr.y );
  90.             _settextcolor( text.helpc );
  91.             _settextposition( text.helpp, 2 );
  92.             _setbkcolor(text.background);
  93.             _outtext( win.helpbuf );
  94.             _settextcolor( text.input );
  95.             _setbkcolor(text.field);
  96.         }
  97.         switch(c){
  98.             case '\r':                              /* Return or Tab key.  */
  99.             case '\t':
  100.                 if (i == maxx - 1)
  101.                     break;
  102.                 cursor( form[i], OFF, win );
  103.                 i++;
  104.                 cursor( form[i], ON, win );
  105.                 break;
  106.  
  107.             case '\0':              /* Control character has been pressed. */
  108.               c = getch();
  109.               switch(c){
  110.                 case 'M':                            /* Left arrow.        */
  111.                 case 'H':                            /* Up arrow.          */
  112.                     if (i == 0)                      /* Move cursor to     */
  113.                         break;                       /* previous field.    */
  114.                     cursor( form[i], OFF, win );
  115.                     i--;
  116.                     cursor( form[i], ON, win );
  117.                     break;
  118.                 case 'K':                            /* Right arrow.       */
  119.                 case 'P':                            /* Down arrow.        */
  120.                     if (i == maxx - 1)               /* Move cursor to next*/
  121.                         break;                       /* field.             */
  122.                     cursor( form[i], OFF, win );
  123.                     i++;
  124.                     cursor( form[i], ON, win );
  125.                     break;
  126.                 case 'Q':                            /* Page Down.         */
  127.                     kontinue = 0;                    /* Exit edit mode.    */
  128.                     key = 1;
  129.                     break;
  130.                 case 'I':                            /* Page Up.           */
  131.                     kontinue = 0;                    /* Exit edit mode.    */
  132.                     key = 2;
  133.                     break;
  134.                 case 'v':                            /* Ctrl + Page Down   */
  135.                     kontinue = 0;                    /* Exit edit mode.    */
  136.                     key = 3;
  137.                     break;
  138.                 case 'G':                            /* Home               */
  139.                     kontinue = 0;                    /* Exit edit mode.    */
  140.                     key = 4;
  141.                     break;
  142.                 case 'O':                            /* End                */
  143.                     kontinue = 0;                    /* Exit edit mode.    */
  144.                     key = 5;
  145.                     break;
  146.               }
  147.         }
  148.     }
  149.     _settextwindow( win.upl.x, win.upl.y, win.lowr.x, win.lowr.y);
  150.     _setbkcolor( text.background );
  151.     i = j = 0;
  152.     while( j < maxx ){
  153.         while (t[i].type == 0)
  154.             i++;
  155.         strcpy(t[i].indata, form[j].indata);
  156.         j++;
  157.         i++;
  158.     }
  159. return key;
  160. }
  161.  
  162.  
  163.